home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jtext.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  5.2 KB  |  178 lines

  1. # jtext.tcl - constrained interface to Text widget
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non-profit, noncommercial use.
  5.  
  6. # TO DO:
  7. #   logging
  8. #   integrate more tightly w/jtexttags (so can log tags as well)
  9.  
  10. ######################################################################
  11. # global variables:
  12. #
  13. global J_PREFS env
  14. if {! [info exists J_PREFS(typeover)]} {set J_PREFS(typeover) 1}
  15. #
  16. ######################################################################
  17.  
  18. ######################################################################
  19. # j:text:insert w text -
  20. #   insert text into w at insert point
  21. #   * detects if tags are being used and uses j:tag:insert_string 
  22. #   * handles deletion of selection if needed
  23. ######################################################################
  24. ### PROBLEM - checking J_PREFS(typeover) shouldn't really be here; it's
  25. ###   only appropriate if a user event is generating the text to insert
  26.  
  27. proc j:text:insert_string { w text } {
  28.   j:debug
  29.   global j_text
  30.   global j_tag
  31.   global J_PREFS
  32.   
  33.   # in typeover mode, all insertions replace selection:
  34.   if $J_PREFS(typeover) {
  35.     if [j:text:insert_touches_selection $w] {    ;# else might be off-screen!
  36.       j:text:delete $w sel.first sel.last
  37.     }
  38.   }
  39.   
  40.   # if we're using tagged-insertion...
  41.   if [info exists j_tag(tags,$w)] {        ;# using tagged text
  42.     j:tag:insert_string $w $text
  43.   } else {
  44.     set start [$w index insert]            ;# ????? USED ?????
  45.     $w insert insert $text
  46.     $w yview -pickplace insert
  47.   }
  48.   set j_text(dirty,$w) 1
  49. }
  50.  
  51. ######################################################################
  52. # j:text:move w index -
  53. #   move insert mark in w to index
  54. ######################################################################
  55.  
  56. proc j:text:move { w index } {
  57.   j:debug
  58.   $w mark set insert $index
  59.   $w yview -pickplace insert
  60. }
  61.  
  62. ######################################################################
  63. # j:text:delete w from to -
  64. #   delete from index from to index to in w
  65. ######################################################################
  66.  
  67. proc j:text:delete { w from to } {
  68.   j:debug
  69.   j:text:move $w $from
  70.   $w delete $from $to
  71.   set j_text(dirty,$w) 1
  72. }
  73.  
  74. ######################################################################
  75. # j:text:replace w from to string -
  76. #   replace range with string, preserving tags at from
  77. ######################################################################
  78.  
  79. proc j:text:replace { w from to string } {
  80.   set start [$w index $from]
  81.   set tags [$w tag names $from]
  82.   
  83.   $w mark set insert $from
  84.   $w delete insert $to
  85.   $w insert insert $string
  86.   
  87.   foreach tag [$w tag names $start] {
  88.     $w tag remove $tag $start insert
  89.   }
  90.   foreach tag $tags {
  91.     $w tag add $tag $start insert
  92.   }
  93.   set j_text(dirty,$w) 1
  94. }
  95.  
  96. ######################################################################
  97. # j:text:mark_dirty w - mark widget w as dirty (modified)
  98. ######################################################################
  99.  
  100. proc j:text:mark_dirty { w } {
  101.   global j_text
  102.   set j_text(dirty,$w) 1
  103. }
  104.  
  105. ######################################################################
  106. # j:text:mark_clean w - mark widget w as clean (unmodified)
  107. ######################################################################
  108.  
  109. proc j:text:mark_clean { w } {
  110.   global j_text
  111.   set j_text(dirty,$w) 0
  112. }
  113.  
  114. ######################################################################
  115. # j:text:is_dirty w -
  116. #   return 1 if w is dirty (modified) else 0
  117. #   (returns 1 if w hasn't been set dirty or clean)
  118. ######################################################################
  119.  
  120. proc j:text:is_dirty { w } {
  121.   global j_text
  122.   if [info exists j_text(dirty,$w)] {
  123.     return $j_text(dirty,$w)
  124.   } else {
  125.     return 1
  126.   }
  127. }
  128.  
  129. ######################################################################
  130. # j:selection_if_any - return selection if it exists, else {}
  131. #   this is from kjx@comp.vuw.ac.nz (R. James Noble)
  132. #   defined elsewhere, but copied here so the bindings libraries
  133. #   don't depend on jtkutils
  134. ######################################################################
  135.  
  136. if {[info procs j:selection_if_any] == {}} {
  137.   proc j:selection_if_any {} {
  138.     if {[catch {selection get} s]} {return ""} {return $s}
  139.   }
  140. }
  141.  
  142. ######################################################################
  143. # j:text:has_selection t -
  144. #   return true if selection made in t, else false
  145. ######################################################################
  146.  
  147. proc j:text:has_selection { t } {
  148.   set selrange [$t tag nextrange sel 1.0]
  149.   
  150.   if {"x$selrange" == "x"} {                    ;# returns {} if none
  151.     return 0
  152.   } else {
  153.     return 1
  154.   }
  155. }
  156.  
  157. ######################################################################
  158. # j:text:insert_touches_selection t -
  159. #   return true if selection exists in t and insert is inside or next
  160. #   to it (as will be the case if you just made the selection with
  161. #   the mouse)
  162. ######################################################################
  163.  
  164. proc j:text:insert_touches_selection { t } {
  165.   if {! [j:text:has_selection $t]} {        ;# no selection
  166.     return 0
  167.   }
  168.   if [$t compare insert < sel.first] {        ;# insert before selection
  169.     return 0
  170.   }
  171.   if [$t compare insert > sel.last] {        ;# insert after selection
  172.     return 0
  173.   }
  174.   return 1
  175. }
  176.  
  177.